# coding:utf-8
import requests
import json
import MySQLdb
import time
import os



# 下载图片
def img_download(img_name):
    net_img_path = "https://img-shefavors.oss-us-west-1.aliyuncs.com/"+img_name
    print(net_img_path)
    local_img_path = 'C:\\juromeocommentimg2\\'
    os.makedirs(local_img_path, exist_ok=True)
    r = requests.get(net_img_path)
    with open(local_img_path+img_name, 'wb') as f:
        f.write(r.content)

# 得到每个链接的所有评论
def all_images():
    db = MySQLdb.connect("192.168.21.238", "root", "Wang123app", "juromeo_comment", charset='utf8')
    cursor = db.cursor()
    #sql
    sql = "SELECT `images` FROM `comments_origin` WHERE `images` !=''"
    try:
       # 执行SQL语句
       cursor.execute(sql)
       # 获取所有记录列表
       results = cursor.fetchall()
       for row in results:
            image = row[0]
            img_download(image)
            #time.sleep(1)

    except:
       print("Error: unable to fecth data")

    # 关闭数据库连接
    db.close()

all_images()


